package com.shushan.base;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shushan.shushanbao.R;


/**
 * @ClassName: NavigationBaseActivity
 * @Desciption: 带标题栏的基类
 * @author: yichaohua
 * @date: 2017-12-29
 */
public class NavigationBaseActivity extends BaseActivity {

    private static final String TAG = "NavigationBaseActivity";

    private ImageView backImageView;
    private TextView titleTextView;//标题
    private Button rightButton;
    private TextView rightTextView;
    private RelativeLayout titleLayou;
    private FrameLayout mContentLayout;

    //是否沉浸状态栏
    private boolean isStatusBar = true;
    //是否允许全屏
    private boolean isFullScreen = false;
    //是否禁止旋转屏幕
    private boolean isScreenRoate = true;

    private ScreenManager screenManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();

        super.setContentView(R.layout.abstract_title_activity);

        setUpViews();
    }

    private void initWindow() {
        screenManager = ScreenManager.getInstance();
        screenManager.setStatusBar(isStatusBar, this);
        screenManager.setScreenRoate(isScreenRoate, this);
        screenManager.setFullScreen(isFullScreen, this);
    }

    private void setUpViews() {
        titleLayou = (RelativeLayout) findViewById(R.id.actionbar_layout);
        backImageView = (ImageView)findViewById(R.id.actionbar_left_back_img);
        titleTextView = (TextView)findViewById(R.id.actionbar_title_tv);
        rightButton = (Button) findViewById(R.id.actionbar_right_btn);
        rightTextView = (TextView) findViewById(R.id.actionbar_right_tv);
        mContentLayout = (FrameLayout)findViewById(R.id.content_layout);

        backImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onLeftBackward();
            }
        });

        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightForward();
            }
        });

    }

    /**
     * 是否设置沉浸状态栏
     * @param statusBar
     */
    public void setStatusBar(boolean statusBar) {
        isStatusBar = statusBar;
    }

    /**
     * 是否设置全屏
     * @param fullScreen
     */
    public void setFullScreen(boolean fullScreen) {
        isFullScreen = fullScreen;
    }

    /**
     * 是否设置旋转屏幕
     * @param screenRoate
     */
    public void setScreenRoate(boolean screenRoate) {
        isScreenRoate = screenRoate;
    }

    /**
     * 左边返回按钮点击后触发
     */
    protected void onLeftBackward() {
        onBackAction();
    }

    /**
     * 右边点击后触发
     */
    protected void onRightForward() {

    }

    @Override
    public void setTitle(int titleId) {
        titleTextView.setText(titleId);
    }

    @Override
    public void setTitle(CharSequence title) {
        titleTextView.setText(title);
    }

    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        onContentChanged();
    }

    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view,params);
        onContentChanged();
    }

    public void setTitleBgResource(int bgResId) {
        titleLayou.setBackgroundResource(bgResId);
    }


    public void hideBack(){
        backImageView.setVisibility(View.GONE);
    }

    public void setRightButton(int titleId){
        rightButton.setVisibility(View.VISIBLE);
        rightTextView.setVisibility(View.GONE);
        rightButton.setText(titleId);
    }

    public void setRightButton(CharSequence title){
        if(!TextUtils.isEmpty(title)){
            rightButton.setVisibility(View.VISIBLE);
            rightTextView.setVisibility(View.GONE);
            rightButton.setText(title);
        }
    }

    public void setRightText(int titleId){
        rightButton.setVisibility(View.GONE);
        rightTextView.setVisibility(View.VISIBLE);
        rightTextView.setText(titleId);
    }

    public void setRightText(CharSequence title){
        if(!TextUtils.isEmpty(title)){
            rightButton.setVisibility(View.GONE);
            rightTextView.setVisibility(View.VISIBLE);
            rightTextView.setText(title);
        }
    }
}
